package treePackage;

import java.util.HashMap;
import java.util.Map;
import parser.Block;

public class Node {

	private String value;
	private Map<String, Block> blocks;
	private Node left_son;
	private Node right_son;

	/**
	 * Variables for Tree to FP.
	 */
	private double BB_FP_Hor;
	private double BB_FP_Ver;

	public Node(String value, Node left_son, Node right_son) {
		this.value = value;
		this.left_son = left_son;
		this.right_son = right_son;

		if (value != null && value.equals("H")) {
			this.blocks = new HashMap<String, Block>();
			this.BB_FP_Hor = max(this.left_son.BB_FP_Hor,
					this.right_son.BB_FP_Hor);
			this.BB_FP_Ver = this.left_son.BB_FP_Ver + this.right_son.BB_FP_Ver;
			for (Block block : (this.right_son.blocks).values()) {
				double temp = block.getY_coordinate();
				block.setY_coordinate(temp + (this.left_son.BB_FP_Ver));
			}
			blocks.putAll(this.left_son.blocks);
			blocks.putAll(this.right_son.blocks);
		} else {
			if (value != null && value.equals("V")) {
				this.blocks = new HashMap<String, Block>();
				this.BB_FP_Ver = max(this.left_son.BB_FP_Ver,
						this.right_son.BB_FP_Ver);
				this.BB_FP_Hor = this.left_son.BB_FP_Hor
						+ this.right_son.BB_FP_Hor;
				for (Block block : (this.right_son.blocks).values()) {
					double temp = block.getX_coordinate();
					block.setX_coordinate(temp + (this.left_son.BB_FP_Hor));
				}
				blocks.putAll(this.left_son.blocks);
				blocks.putAll(this.right_son.blocks);
			}

			else {
				if (value == null) {
					this.blocks = null;
					this.BB_FP_Hor = 0.0;
					this.BB_FP_Ver = 0.0;
				}
			}
		}
	}

	private double max(double a, double b) {
		if (a > b)
			return a;

		return b;
	}

	public Node(String value, Node left_son, Node right_son, Block block) {
		this.value = value;
		this.left_son = left_son;
		this.right_son = right_son;
		this.blocks = new HashMap<String, Block>();
		block.setX_coordinate(block.getHorizontalLength() / 2.0);
		block.setY_coordinate(block.getVerticalLength() / 2.0);
		blocks.put(block.getBlockName(), block);
		this.BB_FP_Hor = block.getHorizontalLength();
		this.BB_FP_Ver = block.getVerticalLength();
	}

	public String getValue() {
		if (value.length() == 1)
			return value;
		return String.valueOf(value.charAt(1));
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Node getLeft_son() {
		return left_son;
	}

	public void setLeft_son(Node left_son) {
		this.left_son = left_son;
	}

	public Node getRight_son() {
		return right_son;
	}

	public void setRight_son(Node right_son) {
		this.right_son = right_son;
	}

	public double getBB_FP_Hor() {
		return BB_FP_Hor;
	}

	public void setBB_FP_Hor(double bB_FP_Hor) {
		BB_FP_Hor = bB_FP_Hor;
	}

	public double getBB_FP_Ver() {
		return BB_FP_Ver;
	}

	public void setBB_FP_Ver(double bB_FP_Ver) {
		BB_FP_Ver = bB_FP_Ver;
	}

	public Map<String, Block> getBlocks() {
		return blocks;
	}

	public void setBlocks(Map<String, Block> blocks) {
		this.blocks = blocks;
	}
}